home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTLex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  3.0 KB  |  143 lines

  1.  
  2. /* MODULE                            HTLex.c
  3. **        LEXICAL ANALYSOR
  4. **
  5. ** AUTHORS:
  6. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  7. **
  8. ** HISTORY:
  9. **
  10. **
  11. ** BUGS:
  12. **
  13. **
  14. */
  15.  
  16. #include "HTUtils.h"
  17. #include "HTAAUtil.h"
  18. #include "HTLex.h"    /* Implemented here */
  19.  
  20. #include "LYLeaks.h"
  21.  
  22. /*
  23. ** Global variables
  24. */
  25. PUBLIC char lex_buffer[40];    /* Read lexical string        */
  26. PUBLIC int lex_line = 1;    /* Line number in source file    */
  27.  
  28.  
  29. /*
  30. ** Module-wide variables
  31. */
  32. PRIVATE int lex_cnt;
  33. PRIVATE BOOL lex_template;
  34. PRIVATE LexItem lex_pushed_back = LEX_NONE;
  35. PRIVATE FILE *cache = NULL;
  36.  
  37.  
  38. PUBLIC void unlex ARGS1(LexItem, lex_item)
  39. {
  40.     lex_pushed_back = lex_item;
  41. }
  42.  
  43.  
  44. PUBLIC LexItem lex ARGS1(FILE *, fp)
  45. {
  46.     int ch;
  47.  
  48.     if (fp != cache) {    /* This cache doesn't work ok because the system  */
  49.     cache = fp;    /* often assign same FILE structure the next open */
  50.     lex_line = 1;    /* file. So, if there are syntax errors in setup  */
  51.     }            /* files it may confuse things later on.      */
  52.  
  53.     if (lex_pushed_back != LEX_NONE) {
  54.     LexItem ret = lex_pushed_back;
  55.     lex_pushed_back = LEX_NONE;
  56.     return ret;
  57.     }
  58.  
  59.     lex_cnt = 0;
  60.     lex_template = NO;
  61.  
  62.     for(;;) {
  63.     switch (ch = getc(fp)) {
  64.       case EOF:
  65.       case ' ':
  66.       case '\t':
  67.       case '\r':
  68.       case '\n':
  69.       case ':':
  70.       case ',':
  71.       case '(':
  72.       case ')':
  73.       case '@':
  74.         if (lex_cnt > 0) {
  75.         if (ch != EOF) ungetc(ch,fp);
  76.         if (lex_template) return LEX_TMPL_STR;
  77.         else          return LEX_ALPH_STR;
  78.         }
  79.         else switch(ch) {
  80.           case EOF:        return LEX_EOF;        break;
  81.           case '\n':
  82.         lex_line++;    return LEX_REC_SEP;    break;
  83.           case ':':        return LEX_FIELD_SEP;    break;
  84.           case ',':        return LEX_ITEM_SEP;    break;
  85.           case '(':        return LEX_OPEN_PAREN;    break;
  86.           case ')':        return LEX_CLOSE_PAREN;    break;
  87.           case '@':        return LEX_AT_SIGN;    break;
  88.           default:    ;    /* Leading white space ignored (SP,TAB,CR) */
  89.         }
  90.         break;
  91.       default:
  92.         lex_buffer[lex_cnt++] = ch;
  93.         lex_buffer[lex_cnt] = (char)0;
  94.         if ('*' == ch) lex_template = YES;
  95.     } /* switch ch */
  96.     } /* forever */
  97. }
  98.  
  99.  
  100. PUBLIC char *lex_verbose ARGS1(LexItem, lex_item)
  101. {
  102.     static char msg[100];
  103.  
  104.     switch (lex_item) {
  105.       case LEX_NONE:        /* Internally used    */
  106.     return "NO-LEX-ITEM";
  107.     break;
  108.       case LEX_EOF:        /* End of file        */
  109.     return "end-of-file";
  110.     break;
  111.       case LEX_REC_SEP:        /* Record separator    */
  112.     return "record separator (newline)";
  113.     break;
  114.       case LEX_FIELD_SEP:    /* Field separator    */
  115.     return "field separator ':'";
  116.     break;
  117.       case LEX_ITEM_SEP:    /* List item separator    */
  118.     return "item separator ','";
  119.     break;
  120.       case LEX_OPEN_PAREN:    /* Group start tag    */
  121.     return "'('";
  122.     break;
  123.       case LEX_CLOSE_PAREN:    /* Group end tag    */
  124.     return "')'";
  125.     break;
  126.       case LEX_AT_SIGN:        /* Address qualifier    */
  127.     return "address qualifier '@'";
  128.     break;
  129.       case LEX_ALPH_STR:    /* Alphanumeric string    */
  130.     sprintf(msg, "alphanumeric string '%s'", lex_buffer);
  131.     return msg;
  132.     break;
  133.       case LEX_TMPL_STR:    /* Template string    */
  134.     sprintf(msg, "template string '%s'", lex_buffer);
  135.     return msg;
  136.     break;
  137.       default:
  138.     return "UNKNOWN-LEX-ITEM";
  139.     break;
  140.     }
  141. }
  142.  
  143.